package com.tz.entity;

import java.io.Serializable;
import java.util.Set;

/**
 * Created by heziqian on 2016/10/21.
 */
public class Cart implements Serializable {

	private static final long serialVersionUID = 3636515614218175193L;
	private Set<OrderItem> items;
	private double totalPrice;
	private int count;

	public Cart() {
		super();
	}

	public Cart(Set<OrderItem> items, double totalPrice, int count) {
		super();
		this.items = items;
		this.totalPrice = totalPrice;
		this.count = count;
	}

	public Set<OrderItem> getItems() {
		return items;
	}

	public void setItems(Set<OrderItem> items) {
		this.items = items;
	}

	public double getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	
	public void addCart(OrderItem item){
		 items=this.getItems();
		 boolean flag=true;
		 
		if(items.size()>0){
		for (OrderItem t : items) {
			if(t.getBook().getBookId().equals(item.getBook().getBookId())){
				t.setCount(t.getCount()+1);
				t.setTotalPrice(t.getCount()*t.getBook().getPrice());
			   flag=false;
			}
		}
				if(flag){
				items.add(item);
				}
			
		}else{	
		items.add(item);
		}
		
		this.setItems(items);
		this.setCount(this.getCount()+1);
		this.setTotalPrice(this.getTotalPrice()+item.getBook().getPrice());
	}


}
